﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace TheVerge.Phone.Controls
{
    public partial class SuperImageControl : UserControl
    {

        public bool IsTransformable
        {
            get { return (bool)GetValue(IsTransformableProperty); }
            set { SetValue(IsTransformableProperty, value); }
        }
        public static readonly DependencyProperty IsTransformableProperty =
            DependencyProperty.Register("IsTransformable", typeof(bool), typeof(SuperImageControl), new PropertyMetadata(false));

        public ImageSource Source
        {
            get { return (ImageSource)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(ImageSource), typeof(SuperImageControl), new PropertyMetadata(null, SourceChanged));

        private static void SourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var sic = d as SuperImageControl;
            sic.ResetControl();
            sic.overlayImage.Source = e.OldValue as ImageSource;
            sic.overlay.ShowQuick();
        }

        public SuperImageControl()
        {
            InitializeComponent();
        }

        private double initialScale;
        private void OnPinchStarted(object sender, Microsoft.Phone.Controls.PinchStartedGestureEventArgs e)
        {
            if (IsTransformable)
            {
                initialScale = transform.ScaleX;
            }
        }

        private void OnPinchDelta(object sender, Microsoft.Phone.Controls.PinchGestureEventArgs e)
        {
            if (IsTransformable)
            {
                var ratio = initialScale * e.DistanceRatio;
                if (CanScale(ratio))
                {
                    transform.ScaleX = ratio;
                    transform.ScaleY = ratio;
                }
            }
        }

        private void GestureListener_DragDelta(object sender, Microsoft.Phone.Controls.DragDeltaGestureEventArgs e)
        {
            if (IsTransformable)
            {
                var newX = transform.TranslateX + e.HorizontalChange;
                var newY = transform.TranslateY + e.VerticalChange;

                if (CanTransform(newX, newY))
                {
                    transform.TranslateX = newX;
                    transform.TranslateY = newY;
                }
            }
        }

        private bool CanScale(double ratio)
        {
            return ratio < 3 && ratio > 0.25;
        }
        
        private bool CanTransform(double newX, double newY)
        {
            return true;
        }

        private void Image_DoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            //if (IsTransformable)
            //{
            //    ResetControl();
            //}
            //else
            //{
            //    originalTop = Canvas.GetTop(img);
            //    originalLeft = Canvas.GetLeft(img);

            //    var gt = img.TransformToVisual(null);
            //    var p = gt.Transform(new Point());
            //    Canvas.SetTop(img, p.Y * -1);
            //    Canvas.SetLeft(img, p.X * -1);
            //    IsTransformable = true;
            //}
        }

        private void ResetControl()
        {
            transform.ScaleX = transform.ScaleY = 1;
            transform.TranslateX = transform.TranslateY = 0;
            IsTransformable = false;
        }

        private void Image_ImageOpened(object sender, RoutedEventArgs e)
        {
            overlay.Hide();
        }
    }
}
